/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-04-14
 * Time: 23:31
 */
public class Main {
    public static void main(String[] args) {
        // 原始字符串
        String originalStr = "Hello";

        /* 一：String 转 StringBuilder */
        // 方法1：通过构造方法转换
        StringBuilder sbuil1 = new StringBuilder(originalStr); // 直接传入String对象构造StringBuilder

        // 方法2：通过append()方法转换
        StringBuilder sbuil2 = new StringBuilder(); // 创建空StringBuilder
        sbuil2.append(originalStr); // 将String内容追加到StringBuilder

        System.out.println("String转StringBuilder结果:");
        System.out.println("构造方法方式: " + sbuil1); // Hello
        System.out.println("append方式: " + sbuil2); // Hello

        /* 二：StringBuilder 转 String */
        // 修改StringBuilder内容
        sbuil1.append(" World!"); // 修改StringBuilder对象

        // 转换为String类型
        String newStr = sbuil1.toString(); // 调用toString()方法获取String对象

        System.out.println("\nStringBuilder转String结果:");
        System.out.println(newStr); // Hello World!
    }
}
